<template>
  <a-modal
    :title="`管理员${title}`"
    :width="640"
    destroyOnClose
    centered
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit('id')"
    @cancel="visible=!visible"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="主键ID" v-show="model.id" >
          <a-input v-decorator="['id', { initialValue: model.id }]" disabled />
        </a-form-item>
        <a-form-item label="账号">
          <a-input v-decorator="['userCode', {initialValue: model.userCode,rules: [{ required: true, min: 3, max: 15, message: '账号长度3到15位！' }]}]"/>
        </a-form-item>
        <a-form-item label="用户姓名">
          <a-input v-decorator="['userName', {initialValue: model.userName,rules: [{ required: true, message: '用户姓名不能空！' }]}]"/>
        </a-form-item>
        <a-form-item label="密码">
          <a-input-password v-decorator="['password', {initialValue: model.password,rules: [{ required: true, message: '密码不能空！' }]}]"/>
        </a-form-item>
        <a-form-item label="手机号码">
          <a-input v-decorator="['mobile', {initialValue: model.mobile,rules: [{ required: true, message: '手机号码不能为空！' }]}]"/>
        </a-form-item>
        <a-form-item label="邮箱地址">
          <a-input v-decorator="['email', {initialValue: model.email,rules: [{ required: true, message: '邮箱不能为空！' }]}]"/>
        </a-form-item>
        <a-form-item label="状态">
          <a-select
            show-search
            placeholder="请选择状态"
            v-decorator="['state', {initialValue: model.state?model.state:0,rules:[{ required: true, message: '状态不能为空！' }]}]">
            <a-select-option v-for="(value, key) in this.switch_state" :key="parseInt(key)">
              ({{ key }}){{ value.dictValue }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="角色">
          <a-select
            placeholder="请选择角色"
            mode="multiple"
            v-decorator="['roleId', {initialValue: model.roles?model.roles.map(item=>item.id):[],rules:[{ required: true, message: '角色不能为空！' }]}]"
            style="width: 100%">
            <a-select-option v-for="role in roles" :key="role.id">{{ role.roleName }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import md5 from 'md5'
  import detailMixins from '@/utils/mixins/detailMixins'
  import { getAllRole } from '@/api/system/role'
  import { getInfo, addInfo, editInfo } from '@/api/system/admin'
  export default {
    mixins: [detailMixins],
    data () {
      return {
        roles: [],
        switch_state: {}
      }
    },
    created () {
      this.getDictValue('switch_state').then(res => {
        this.switch_state = res.data
      })
      getAllRole().then(res => {
        if (res.code === '0000') {
          this.roles = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    methods: {
      info (record) {
        return getInfo(record)
      },
      filterForm (values) {
        if (JSON.stringify(values.password) !== JSON.stringify(this.model.password)) {
          values.password = md5(values.password)
        }
        return values
      },
      add (values) {
        return addInfo(values)
      },
      edit (values) {
        return editInfo(values)
      }
    }
  }
</script>
<style scoped>
</style>
